
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

dojo.provide("nanosn.widgets.crud._Grid");
dojo.require("dijit._Widget");
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojox.data.JsonRestStore");
dojo.require("dojox.rpc.Service");

dojo.declare("nanosn.widgets.crud._Grid", dijit._Widget,
{

    /*
     *
     * constructor
     * postMixInProperties
     * buildRendering
     * postCreate
     * startup
     * destroy
     *
     */
    _grid: null,
    _structure: null,
    _store: null,
    url: null,
    constructor: function(args)
    {
        if(djConfig.isDebug)
        {
            console.log("constructor");
        }
    },

    postMixInProperties: function()
    {
        if(djConfig.isDebug)
        {
            console.log("postMixInProperties");
        }
        
        //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
        var xhrArgs = {
            url: new String(this.url + "/structure"),
            sync: true,
            handleAs: "json",
            error: function(error){
                targetNode.innerHTML = "An unexpected error occurred: " + error;
            }
        }

        //Call the asynchronous xhrGet
        var deferred = dojo.xhrGet(xhrArgs);
        deferred.addCallback(dojo.hitch(this,"setStructure"));

    //        this.inherited(arguments);
    },


    
    setStructure: function(data)
    {
        this._structure = data;
    },
    
    buildRendering: function()
    {
        if(djConfig.isDebug)
        {
            console.log("buildRendering");
        }
        this._store = new dojox.data.JsonRestStore({
            target: this.url
        });
       
        var args = {
            elasticView: 2,
            rowsPerPage:20,
            store: this._store,
            structure: this._structure,
            rowSelector:"20px"
        };
        
        this._grid = new dojox.grid.DataGrid(args, document.createElement("div"));
        this.domNode = this._grid.domNode;
    },

    postCreate: function()
    {
        if(djConfig.isDebug)
        {
            console.log("postCreate");
        }
        dojo.connect(this._grid, "onRowDblClick", this,function(event)
        {
            this.onRowDblClick(event);
        });
        
        dojo.connect(this._grid, "onRowClick", this, function(event)
        {
            this.onRowClick(event);
        });
    //        this.inherited(arguments);
    },

    startup: function()
    {
        if(djConfig.isDebug)
        {
            console.log("startup");
        }
        //        this.inherited(arguments);
        this._grid.startup();
    },

    distroy: function()
    {
    //disconnect all events
    },
    
    onRowDblClick: function(event)
    {
        //An extension point for this event
    },

    onRowClick: function(event)
    {
        //An extension point for this event
    }
});
